<template>
    <el-row type="flex" class="row-bg" justify="center">
<!--        标题-->
        <el-col :xl="6" :lg="7">
            <h2>欢迎来到VueAdmin管理系统</h2>
            <el-image :src="require('@/assets/bbfeng.png')" style="height: 180px;width: 180px;"></el-image>
            <h4>源码地址</h4>
            <h3>欢迎查看~</h3>
        </el-col>
<!--        分割线-->
        <el-col :span="1">
            <el-divider direction="vertical"></el-divider>
        </el-col>
<!--        表单-->
        <el-col :xl="6" :lg="7">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <div class="code">
                        <el-input v-model="loginForm.code"></el-input>
                        <el-image :src="captchaImg"></el-image>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
                    <el-button @click="resetForm('loginForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script>
  export default {
    name: 'Login',
      data() {
          return {
              loginForm: {
                  username: '',
                  password:'',
                  code:'',
                  token:''
              },
              rules: {
                  username: [
                      { required: true, message: '请输入用户名', trigger: 'blur' },
                  ],
                  password: [
                      { required: true, message: '请输入密码', trigger: 'blur' }
                  ],
                  code: [
                      { required: true, message: '请输入验证码', trigger: 'blur' },
                      { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' }
                  ]
              },
              captchaImg:null
          };
      },
      created() {
        this.getCaptchaImg()
      },
      methods: {
          submitForm(formName) {
              this.$refs[formName].validate((valid) => {
                  if (valid) {
                      this.$axios.post('/login',this.loginForm).then(res => {
                          const jwt = res.headers['authorization']
                          this.$store.commit('SET_TOKEN', jwt);

                          this.$store.commit('SET_HASROUTE', false);

                          this.$message.success('登录成功！');
                          this.$router.push('/index');
                      });
                  } else {
                      this.$message.error('登录失败！');
                      return false;
                  }
              });
          },
          resetForm(formName) {
              this.$refs[formName].resetFields();
          },
          getCaptchaImg () {
              this.$axios.get('/captcha').then(res => {
                  this.loginForm.token = res.data.data.token
                  this.captchaImg = res.data.data.captchaImg
              })
          }
          // result -> code,msg,data
      }
  }
</script>
<style lang="scss" scoped>
    .el-row{
        background-color: #fafafa;
        width: 100%;
        height: 100%;

        display: flex;
        align-items: center;
        text-align: center;
        // 分割线
        .el-divider{
            height: 200px;
        }
        // 表单元素
        .el-form-item{
            width: 380px;
            // 验证码
            .code{
                display: flex;
                .el-input{
                    flex: 2;
                    margin-right: 5px;
                }
                .el-image{
                    border-radius: 3px;
                    flex: 1;
                }

            }
        }
    }
</style>
